package com.bwie.home.backstage.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.home.backstage.entity.RegionEntity;
import com.bwie.home.backstage.service.RegionService;
import com.bwie.home.common.utils.PageUtils;
import com.bwie.home.common.utils.R;



/**
 * 
 *
 * @author sy
 * @email sunlightcs@gmail.com
 * @date 2021-09-10 09:46:41
 */
@RestController
@RequestMapping("backstage/region")
public class RegionController {
    @Autowired
    private RegionService regionService;


    //查询县
    @GetMapping("/getProvincById")
    public R getProvincById(){
        List<RegionEntity> regions = regionService.list(new QueryWrapper<RegionEntity>().eq("parent_id",0));
        return R.ok().put("list",regions);
    }

    @GetMapping("/getCityName/{Name}")
    public R getCityById(@PathVariable String Name){
        RegionEntity reg = regionService.getOne(new QueryWrapper<RegionEntity>().eq("region_name", Name));
        List<RegionEntity> regions = regionService.list(new QueryWrapper<RegionEntity>().eq("parent_id",reg.getRegionId()));
        return R.ok().put("list",regions);
    }


    /**
     * 根据省份名称查询省份
     */
    @GetMapping("/getProvinceByName")
    public R getProvinceByName(@RequestParam("provinceName") String provinceName){
        RegionEntity region = regionService.getOne(new QueryWrapper<RegionEntity>().eq("region_name",provinceName));
        return R.ok().put("data",region);
    }

    /**
     * 获取城市下拉框
     */
    @GetMapping("/getRegionAllCities/{regionId}")
    public R getRegionAllCities(@PathVariable("regionId")Integer regionId){
        List<RegionEntity> regionEntityList = regionService.getRegionAllCities(regionId);
        return R.ok().put("list",regionEntityList);
    }

    /**
     * 获取省份下拉框
     */
    @GetMapping("/getRegionAllProvinces")
    public R getRegionAllProvinces(){
        List<RegionEntity> regionEntityList = regionService.list(new QueryWrapper<RegionEntity>().eq("parent_id",0));
        return R.ok().put("list",regionEntityList);
    }

    /**
     * 获取城市、区县
     */
    @GetMapping("/getCities/{regionId}")
    public R getCities(@PathVariable("regionId")Integer regionId,@RequestParam Map<String, Object> params){
        PageUtils cities = regionService.getCities(regionId,params);
        return R.ok().put("list",cities);
    }

    /**
     * 获取省份
     */
    @GetMapping("/getProvinces")
    public R getProvinces(@RequestParam Map<String, Object> params){
        PageUtils provinces = regionService.getProvinces(params);
        return R.ok().put("list",provinces);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = regionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{regionId}")
    public R info(@PathVariable("regionId") Integer regionId){
		RegionEntity region = regionService.getById(regionId);

        return R.ok().put("region", region);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody RegionEntity region){
		regionService.save(region);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RegionEntity region){
		regionService.updateById(region);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] regionIds){
		regionService.removeByIds(Arrays.asList(regionIds));

        return R.ok();
    }

}
